//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CurrencyValuesDao.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:24:46  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.HashMap;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesm02Currvalue;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class CurrencyValuesDao extends Dao {

    private static final String CLASS_NAME = CurrencyValuesDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * Valida si existe ya un tipo de cambio cuya fecha de inicio este dentro
     * del rango de fechas ingresadas en el tipo de cambio pasado por parametro
     * 
     * @param currValue
     * Debe tener cargado el tipo de cambio, la planta y la fechaIn
     */

    public boolean validRange(Teesm02Currvalue currValue)
            throws PersistenceException {

        String METHOD_NAME = "validRange";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesm02Currvalue result = new Teesm02Currvalue();
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("CurrencyValueInRange",
                Teesm02Currvalue.class);
        query.setParameters(currValue);

        result = (Teesm02Currvalue) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        if (result == null)
            return true;
        return false;
    }

    /**
     * Busca el tipo de cambio valido para el periodo anterior al periodo del
     * tipo de cambio pasado por parametro
     * 
     * @param currValue
     * Debe tener cargado el tipo de cambio, la planta y la fechaIn
     */

    public Teesm02Currvalue getPrevCurrencyValue(Teesm02Currvalue currValue)
            throws PersistenceException {

        String METHOD_NAME = "getPrevCurrencyValue";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesm02Currvalue result = new Teesm02Currvalue();
        Teesm02Currvalue prCurrValue = new Teesm02Currvalue();
        PersistenceManager pm = null;

        prCurrValue.setEesm02PlantCodeC(currValue.getEesm02PlantCodeC());
        prCurrValue.setEesm02DestCurrencyCodeC(currValue
                .getEesm02DestCurrencyCodeC());
        prCurrValue.setEesm02OrigCurrencyCodeC(currValue
                .getEesm02OrigCurrencyCodeC());

        prCurrValue.setEesm02CurrencyValueDateY(this
                .getPrevCurrencyValueFechaIn(currValue));

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findCurrencyValueByCodes",
                Teesm02Currvalue.class);
        query.setParameters(prCurrValue);

        result = (Teesm02Currvalue) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    public Timestamp getPrevCurrencyValueFechaIn(Teesm02Currvalue currValue)
            throws PersistenceException {

        String METHOD_NAME = "getPrevCurrencyValueFechaIn";
        log.entering(CLASS_NAME, METHOD_NAME);

        HashMap<String, Timestamp> result;
        PersistenceManager pm = null;
        Timestamp fechaIn;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("PrevCurrencyValueFechaIn",
                Teesm02Currvalue.class);
        query.setParameters(currValue);

        result = (HashMap<String, Timestamp>) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        fechaIn = result.get("fechaIn-max");
        return fechaIn;
    }

    /**
     * Da de alta el Currency Value pasado por parametro
     * 
     * @param currency
     * Value Debe tener cargado por lo menos los datos de la pk del Currency
     * Value
     */
    public void createCurrencyValue(Teesm02Currvalue currencyValue)
            throws PersistenceException {

        String METHOD_NAME = "createCurrencyValue";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(currencyValue);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Devuelve el currency Value listo para realizarle las actualizaciones
     * pertinentes
     * 
     * @param currValue
     * Currency Value a actualizar
     * @return Currency Value
     * @throws PersistenceException
     */
    public Teesm02Currvalue getCurrencyValueForUpdate(Teesm02Currvalue currValue)
            throws PersistenceException {

        String METHOD_NAME = "getCurrencyValueForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesm02Currvalue result = new Teesm02Currvalue();
        result = (Teesm02Currvalue) super.getObjForUpdate(currValue);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * Elimina de forma definitiva de la base de datos el currency Value pasado
     * por parametro
     * 
     * @param currencyValue
     * Debe haber sido obtenido de la base de datos
     */
    public void deleteCurrencyValue(Teesm02Currvalue currencyValue)
            throws PersistenceException {

        String METHOD_NAME = "deleteCurrencyValue";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        if (tx != null) {
            tx.registerDelete(currencyValue);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Busca CurrencyValues segun currency origen y destino
     * 
     * @param Teesm01Currency
     * @param fecha
     * @return
     * @throws PersistenceException
     */
    public Collection<Teesm02Currvalue> findCurrencyValues(
                                                           Teesm02Currvalue currencyValue,
                                                           Timestamp fecha)
            throws PersistenceException {

        String METHOD_NAME = "findCurrencyValues";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesm02Currvalue> resultCol = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findCurrencyValues", Teesm02Currvalue.class);
        query.setParameters(currencyValue, fecha);

        resultCol = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
    }
}
